function getquery(id) {
    return document.querySelector(id);
}
function getqueryall(id) {
    return document.querySelectorAll(id);
}
function addListener(object, eve, fun) {
    object.addEventListener(eve, fun);
}
function toDup(i) {
    return i >= 10 ? i : "0" + i;
}
function getTimer() {
    var timer = new Date();
    var y = timer.getFullYear();
    var M = timer.getMonth() + 1;
    var d = timer.getDate();
    var h = timer.getHours();
    var m = timer.getMinutes();
    var s = timer.getSeconds();
    return y + "-" + toDup(M) + "-" + toDup(d) + " " + toDup(h) + ":" + toDup(m) + ":" + toDup(s);
}
function color() {
    var r = rem(0, 256);
    var g = rem(0, 256);
    var b = rem(0, 256);
    return 'rgb(' + r + ',' + g + ',' + b + ')';
}
function rem(a, b) {
    return Math.floor(Math.random() * (b - a) + a);
}
function collide(a, b) {
    if (b.offsetLeft + b.offsetWidth - 10 < a.offsetLeft || a.offsetLeft + a.offsetWidth - 10 < b.offsetLeft || b.offsetTop + b.offsetHeight - 10 < a.offsetTop || a.offsetTop + a.offsetHeight - 10 < b.offsetTop) {
        return false;
    } else {
        return true;
    }
}
class BulletTS {
    constructor(x, y, text, sp) {
        this.x = x;
        this.y = y;
        this.text = text;
        this.div = document.createElement("div");
        this.sp = sp;
        this.init();
    }
    init() {
        this.div.style.width = "50px";
        this.div.style.height = "50px";
        this.div.style.position = "absolute";
        this.div.style.borderRadius = "50%";
        this.div.style.display = "flex";
        this.div.style.justifyContent = "center";
        this.div.style.alignItems = "center";
        this.div.style.fontSize = "30px";
        this.div.innerHTML = this.text;
        this.div.style.left = this.x + "px";
        this.div.style.top = this.y + "px";
        playPage.appendChild(this.div);
    }
    tomove() {
        this.div.style.top = this.div.offsetTop + 3 + "px";
        this.div.style.boxShadow = "0 0 " + rem(1, 20) + "px " + color();
    }
}
class Bullet {
    constructor(src, x, y, sleep) {
        this.bul = document.createElement("img");
        this.src = src;
        this.x = x;
        this.y = y;
        this.sleep = sleep;
        this.init();
    }
    init() {
        this.bul.src = this.src;
        this.bul.style.position = "absolute";
        this.bul.style.top = this.y + "px";
        this.bul.style.left = this.x + "px";
        this.bul.style.zIndex = 10;
        playPage.appendChild(this.bul);
    }
    tomove() {
        if (playerPlane.ts == "a") {
            this.bul.style.top = this.bul.offsetTop - this.sleep + "px";
            this.bul.style.left = this.bul.offsetLeft + rem(-20, 20) + "px";
        } else if (playerPlane.ts == "A") {
            this.bul.style.top = this.bul.offsetTop - this.sleep + "px";
            this.bul.style.left = this.bul.offsetLeft + rem(-30, 30) + "px";
        } else {
            this.bul.style.top = this.bul.offsetTop - this.sleep + "px";
        }
    }
}
class EnemyPlane {
    constructor(src, x, y, sleep, hp, srcp, srcb) {
        this.img = document.createElement("img");
        this.src = src;
        this.x = x;
        this.y = y;
        this.sleep = sleep;
        this.hp = hp;
        this.isDel = false;
        this.DelTime = 100;
        this.srcp = srcp;
        this.srcb = srcb;
        this.init();
        this.hpdiv = document.createElement("div");
    }
    init() {
        this.img.src = this.src;
        this.img.style.position = "absolute";
        this.img.style.top = this.y + "px";
        this.img.style.left = this.x + "px";
        playPage.appendChild(this.img);
    }
    tomove() {
        this.img.style.top = this.img.offsetTop + this.sleep + "px";
        this.hpdiv.style.top = this.img.offsetTop + this.sleep + "px";
        this.hpdiv.style.left = this.img.offsetLeft + this.img.offsetWidth / 2 - this.hp / 2 + "px";
    }
    tohp() {
        this.hpdiv.style.width = this.hp + "px";
        this.hpdiv.style.height = "5px";
        this.hpdiv.style.borderRadius = "5px";
        this.hpdiv.style.position = "absolute";
        this.hpdiv.style.backgroundColor = "red";
        playPage.appendChild(this.hpdiv);
    }
}
class PlayerPlane {
    constructor(src, x, y, ts, sp) {
        this.img = document.createElement("img");
        this.src = src;
        this.x = x;
        this.y = y;
        this.ts = ts;
        this.sp = sp;
        this.jiguan = 0;
        this.wudi = false;
    }
    init() {
        this.img.src = this.src;
        this.img.style.position = "absolute";
        this.img.style.top = this.y + "px";
        this.img.style.left = this.x + "px";
        playPage.appendChild(this.img);
    }
    toMove(x, y) {
        // if (x - this.img.offsetWidth / 2 > 0 && x - this.img.offsetWidth / 2 < playPage.offsetWidth - this.img.offsetWidth && y - this.img.offsetHeight / 2 > 0 && y - this.img.offsetHeight / 2 < playPage.offsetHeight - this.img.offsetHeight) {

        // }
        this.img.style.left = x - this.img.offsetWidth / 2 + "px";
        this.img.style.top = y - this.img.offsetHeight / 2 + "px";
    }
    toShooting() {
        if (this.ts == "m" || this.ts == "a" || this.ts == "f") {
            var b = new Bullet("./images/bullet1.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 2, this.img.offsetTop - 9, 12);
            bulletarray.push(b);
        } else if (this.ts == "M") {
            var b = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 2, this.img.offsetTop - 9, 12);
            bulletarray.push(b);
        } else if (this.ts == "s") {
            var b1 = new Bullet("./images/bullet1.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 2, this.img.offsetTop - 9, 26);
            bulletarray.push(b1);
            var b2 = new Bullet("./images/bullet1.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 16, this.img.offsetTop - 9, 25);
            bulletarray.push(b2);
            var b3 = new Bullet("./images/bullet1.png", this.img.offsetLeft + this.img.offsetWidth / 2 + 12, this.img.offsetTop - 9, 25);
            bulletarray.push(b3);
        } else if (this.ts == "A" || this.ts == "F") {
            var b1 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 2, this.img.offsetTop - 9, 26);
            bulletarray.push(b1);
            var b2 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 16, this.img.offsetTop - 9, 25);
            bulletarray.push(b2);
            var b3 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 + 12, this.img.offsetTop - 9, 25);
            bulletarray.push(b3);
        } else if (this.ts == "S") {
            var b1 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 2, this.img.offsetTop - 9, 45);
            bulletarray.push(b1);
            var b2 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 16, this.img.offsetTop - 9, 42);
            bulletarray.push(b2);
            var b3 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 + 12, this.img.offsetTop - 9, 42);
            bulletarray.push(b3);
            var b4 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 30, this.img.offsetTop - 9, 40);
            bulletarray.push(b4);
            var b5 = new Bullet("./images/bullet1b.png", this.img.offsetLeft + this.img.offsetWidth / 2 + 26, this.img.offsetTop - 9, 40);
            bulletarray.push(b5);
        } else if (this.ts == "v") {
            var b = new Bullet("./images/bullet2.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 60, this.img.offsetTop - 80, 8);
            b.bul.style.width = b.bul.offsetWidth + "px";
            b.bul.style.width = b.bul.offsetHeight + "px";
            bulletarray.push(b);
        } else if (this.ts == "V") {
            var b = new Bullet("./images/bullet2.png", this.img.offsetLeft + this.img.offsetWidth / 2 - 79, this.img.offsetTop - 115, 8);
            b.bul.style.width = b.bul.offsetWidth * 1.1 + "px";
            b.bul.style.width = b.bul.offsetHeight * 1.1 + "px";
            bulletarray.push(b);
        } else if (this.ts == "l") {
            this.jiguan++;
            if (this.jiguan == 1) {
                for (var j = 0; j < bulletarray.length; j++) {
                    bulletarray[j].bul.parentNode.removeChild(bulletarray[j].bul);
                    bulletarray.splice(j, 1);
                }
                var b = new Bullet("./images/bulletjiguang.gif", this.img.offsetLeft + this.img.offsetWidth / 2 - 30, this.img.offsetTop - playPage.offsetHeight, 8);
                b.bul.style.height = playPage.offsetHeight + "px";
                b.bul.style.width = "100px";
                bulletarray.push(b);
            }
        } else if (this.ts == "L") {
            this.jiguan++;
            if (this.jiguan == 1) {
                for (var j = 0; j < bulletarray.length; j++) {
                    bulletarray[j].bul.parentNode.removeChild(bulletarray[j].bul);
                    bulletarray.splice(j, 1);
                }
                var b = new Bullet("./images/bulletjiguang.gif", this.img.offsetLeft + this.img.offsetWidth / 2 - 30, this.img.offsetTop - playPage.offsetHeight, 8);
                b.bul.style.height = playPage.offsetHeight + "px";
                bulletarray.push(b);
            }
        }
    }
    todel(obj) {
        if (collide(this.img, obj)) {
            this.img.parentNode.removeChild(this.img);
            clearInterval(timer);
            setTimeout(function () {
                startPage.style.display = "";
                playPage.style.display = "";
                playPage.style.cursor = "";
                playMose.parentNode.style.display = "";
                playPage.removeEventListener("mousemove", playShootingmove);
            }, 300);
            var l = document.createElement("li");
            l.innerHTML = `<div>-</div><div>${deFen}</div>
            <div>${playTime.innerHTML}</div><div>${playMose.innerHTML}</div><div>${getTimer()}</div>`;
            var li = getqueryall("#box>div:first-child>div:nth-child(4)>ul>li");
            if (li.length == 0) {
                startul.appendChild(l);
                l.children[0].innerHTML = 1;
            } else {
                for (var i = 0; i < li.length; i++) {
                    if (li[i].children[1].innerText < l.children[1].innerText) {
                        startul.insertBefore(l, li[i]);
                        break;
                    } else {
                        startul.appendChild(l);
                    }
                }
            }
            for (var i = 0; i < li.length; i++) {
                li[i].children[0].innerText = i + 1;
            }
        }
    }
}

var startPage = getquery("#box>div:first-child");
var startul = getquery("#box>div:first-child>div:nth-child(4)>ul");
var startBtn = getquery("#box>div:first-child>button:first-child");
var startBtn2 = getquery("#box>div:first-child>button:nth-child(2)");
var startBtn3 = getquery("#box>div:first-child>button:nth-child(3)");
var playPage = getquery("#box>div:nth-child(2)");
var playFen = getquery("#box>div:nth-child(3)>div:nth-child(1)");
var playMose = getquery("#box>div:nth-child(3)>div:nth-child(2)");
var playTime = getquery("#box>div:nth-child(3)>div:nth-child(3)");
var playerPlane;
var bulletarray = [];
var EnemyPlanearr = [];
var deFen = 0;
var bulletbu = false;
var setintimer = 0;
var playtime = 0;
var timer;
var EnemyPlaneSleep;
var EnemyPlanehp;
var EnemyPlanetimer;
var keyspace = false;
var contextmenudiv = false;
var contextmenudivtime = 0;
var contextmenudivt = 2000;
var bulTsarr = [];
var bulTS = [
    { name: "s", sp: 1 },
    { name: "S", sp: 1 },
    { name: "m", sp: 1 },
    { name: "M", sp: 5 },
    { name: "a", sp: 1 },
    { name: "A", sp: 1 },
    { name: "v", sp: 1 },
    { name: "V", sp: 3 },
    { name: "f", sp: 1 },
    { name: "F", sp: 1 },
    { name: "l", sp: 1 },
    { name: "L", sp: 2 },
]
addListener(startBtn, "click", startFu1);
addListener(startBtn2, "click", startFu2);
addListener(startBtn3, "click", startFu3);
function startFu() {
    clearInterval(timer);
    bulletarray = [];
    playPage.innerHTML = "";
    EnemyPlanearr = [];
    deFen = 0;
    setintimer = 0;
    playtime = 0;
    contextmenudivt = 2000;
    contextmenudiv = false;
    contextmenudivtime = 0;
    startPage.style.display = "none";
    playMose.parentNode.style.display = "flex";
    playPage.style.display = "block";
    playPage.style.cursor = "none";
    playerPlane = new PlayerPlane("./images/myplane.gif", 500, 500, "m", 1);
    playerPlane.init();
    timer = setInterval(setInter, 10);
    addListener(playPage, "mousemove", playMoveFu);
}
function startFu1() {
    EnemyPlaneSleep = 1;
    EnemyPlanehp = 1;
    EnemyPlanetimer = 40;
    playMose.innerHTML = startBtn.innerText;
    startFu();
}
function startFu2() {
    EnemyPlaneSleep = 2;
    EnemyPlanehp = 3;
    EnemyPlanetimer = 25;
    playMose.innerHTML = startBtn2.innerText;
    startFu();
}
function startFu3() {
    EnemyPlaneSleep = 3;
    EnemyPlanehp = 6;
    EnemyPlanetimer = 15;
    playMose.innerHTML = startBtn3.innerText;
    startFu();
}
addListener(playPage, "mousedown", playShooting);
function setInter() {
    setintimer++;
    contextmenudivt++;
    playtime = toDup(Math.floor(Math.floor(setintimer / 100) / 60)) + ":" + toDup(Math.floor(setintimer / 100) % 60) + ":" + toDup(setintimer % 100);
    playTime.innerHTML = playtime;
    for (var i = 0; i < bulletarray.length; i++) {
        if (bulletarray[i].bul.style.top > "0px") {
            if (playerPlane.ts == "l") {
                bulletarray[i].bul.style.top = playerPlane.img.offsetTop - playPage.offsetHeight + 20 + "px";
                bulletarray[i].bul.style.left = playerPlane.img.offsetLeft + playerPlane.img.offsetWidth / 2 - 50 + "px";
            } else if (playerPlane.ts == "L") {
                bulletarray[i].bul.style.top = playerPlane.img.offsetTop - playPage.offsetHeight + 20 + "px";
                bulletarray[i].bul.style.left = playerPlane.img.offsetLeft + playerPlane.img.offsetWidth / 2 - 100 + "px";
            } else {
                bulletarray[i].tomove();
            }
        } else {
            if (playerPlane.ts == "l") {
                bulletarray[i].bul.style.top = playerPlane.img.offsetTop - playPage.offsetHeight + 20 + "px";
                bulletarray[i].bul.style.left = playerPlane.img.offsetLeft + playerPlane.img.offsetWidth / 2 - 50 + "px";
            } else if (playerPlane.ts == "L") {
                bulletarray[i].bul.style.top = playerPlane.img.offsetTop - playPage.offsetHeight + 20 + "px";
                bulletarray[i].bul.style.left = playerPlane.img.offsetLeft + playerPlane.img.offsetWidth / 2 - 100 + "px";
            } else {
                bulletarray[i].bul.parentNode.removeChild(bulletarray[i].bul);
                bulletarray.splice(i, 1);
            }
        }
        for (var j = 0; j < EnemyPlanearr.length; j++) {
            if (EnemyPlanearr[j].isDel == false) {
                if (collide(bulletarray[i].bul, EnemyPlanearr[j].img)) {
                    EnemyPlanearr[j].tohp();
                    EnemyPlanearr[j].hp -= playerPlane.sp;
                    EnemyPlanearr[j].img.src = EnemyPlanearr[j].srcp;
                    if (EnemyPlanearr[j].hp <= 0) {
                        if (EnemyPlanearr[j].img.offsetWidth == 34) {
                            deFen += EnemyPlanehp * 2;
                        }
                        if (EnemyPlanearr[j].img.offsetWidth == 46) {
                            deFen += EnemyPlanehp * 4;
                        }
                        if (EnemyPlanearr[j].img.offsetWidth == 110) {
                            deFen += EnemyPlanehp * 20;
                        }
                        playFen.innerHTML = "得分：" + deFen;
                        EnemyPlanearr[j].img.src = EnemyPlanearr[j].srcb;
                        EnemyPlanearr[j].isDel = true;
                        EnemyPlanearr[j].hpdiv.parentNode.removeChild(EnemyPlanearr[j].hpdiv);
                        break;
                    }
                    if (EnemyPlanearr[j].img.offsetTop > playPage.offsetHeight) {
                        EnemyPlanearr[j].hpdiv.parentNode.removeChild(EnemyPlanearr[j].hpdiv);
                    }
                    if (playerPlane.ts == "f" || playerPlane.ts == "F" || playerPlane.ts == "l" || playerPlane.ts == "L") {

                    } else {
                        bulletarray[i].bul.parentNode.removeChild(bulletarray[i].bul);
                        bulletarray.splice(i, 1);
                    }
                    break;
                }
            }
        }
    }
    if (setintimer % rem(100, 1000) == 0) {
        var re = rem(0, bulTS.length);
        var bulTs = new BulletTS(rem(0, playPage.offsetWidth - 100), 1, bulTS[re].name, bulTS[re].sp);
        bulTsarr.push(bulTs);
    }
    if (setintimer % (1 * EnemyPlanetimer) == 0) {
        var enemy = new EnemyPlane("./images/enemy1_fly_1.png", rem(0, playPage.offsetWidth - 34), -24, EnemyPlaneSleep + 2, EnemyPlanehp * rem(2, 20), "./images/enemy1_fly_1.png", "./images/小飞机爆炸.gif");
        EnemyPlanearr.push(enemy);
    }
    if (setintimer % (3 * EnemyPlanetimer) == 0) {
        var enemy = new EnemyPlane("./images/enemy3_fly_1.png", rem(0, playPage.offsetWidth - 46), -60, EnemyPlaneSleep + 1, EnemyPlanehp * rem(20, 50), "./images/中飞机挨打.png", "./images/中飞机爆炸.gif");
        EnemyPlanearr.push(enemy);
    }
    if (setintimer % (10 * EnemyPlanetimer) == 0) {
        var enemy = new EnemyPlane("./images/enemy2_fly_1.png", rem(0, playPage.offsetWidth - 110), -164, EnemyPlaneSleep, EnemyPlanehp * rem(50, 100), "./images/大飞机挨打.png", "./images/大飞机爆炸.gif");
        EnemyPlanearr.push(enemy);
    }
    if (bulletbu == false) {
        if (setintimer % 5 == 0) {
            playerPlane.toShooting();
        }
    }
    for (var i = 0; i < EnemyPlanearr.length; i++) {
        if (EnemyPlanearr[i].img.offsetTop > playPage.offsetHeight || EnemyPlanearr[i].isDel == true) {
            EnemyPlanearr[i].DelTime--;
            if (EnemyPlanearr[i].DelTime <= 0) {
                EnemyPlanearr[i].img.parentNode.removeChild(EnemyPlanearr[i].img);
                EnemyPlanearr.splice(i, 1);
            }
        } else {
            EnemyPlanearr[i].tomove();
            if (playerPlane.wudi == false) {
                playerPlane.todel(EnemyPlanearr[i].img);
            }
        }
    }

    for (i = 0; i < bulTsarr.length; i++) {
        if (bulTsarr[i].div.offsetTop <= playPage.offsetHeight) {
            bulTsarr[i].tomove();
            bulTsarr[i].div.style.left = bulTsarr[i].div.offsetLeft + rem(-2, 2) + "px";
            if (collide(bulTsarr[i].div, playerPlane.img)) {
                playerPlane.ts = bulTsarr[i].text;
                if (playerPlane.ts == "l" || playerPlane.ts == "L") {
                    playerPlane.sp = bulTsarr[i].sp + EnemyPlaneSleep;
                } else {
                    playerPlane.sp = bulTsarr[i].sp + EnemyPlanehp;
                }
                playerPlane.jiguan = 0;
                bulTsarr[i].div.parentNode.removeChild(bulTsarr[i].div);
                bulTsarr.splice(i, 1);
            }
        } else {
            bulTsarr[i].div.parentNode.removeChild(bulTsarr[i].div);
            bulTsarr.splice(i, 1);
        }
    }
    if (contextmenudiv == true) {
        if (setintimer % 50 == 0) {
            contextmenudivtime++;
            var div = document.createElement("img");
            playPage.appendChild(div);
            div.src = "./images/mofaz.png";
            div.style.display = "block";
            div.style.position = "absolute";
            div.style.left = "calc(50% - 0px)";
            div.style.top = "calc(50% - 0px)";
            div.style.borderRadius = "50%";
            div.style.boxShadow = "0 0 5px";
            div.style.width = "0";
            div.style.height = "0";
            div.style.opacity = "0";
            div.style.transition = "all 0.5s";
            setTimeout(function () {
                div.style.width = "500px";
                div.style.height = "500px";
                div.style.left = "calc(50% - 250px)";
                div.style.top = "calc(50% - 250px)";
                div.style.opacity = 1;
            }, 10);
            for (var i = 0; i < EnemyPlanearr.length; i++) {
                EnemyPlanearr[i].hp = 0;
                playerPlane.wudi = true;
            }
            setTimeout(function () {
                div.parentNode.removeChild(div);
            }, 1000);
        }
        if (contextmenudivtime == 10) {
            contextmenudiv = false;
            contextmenudivtime = 0;
            playerPlane.wudi = false;
        }
    }
}
function playMoveFu(e) {
    playerPlane.toMove(e.pageX, e.pageY);
}
function playShooting(e) {
    bulletbu = true;
    e.cancleBubble = true;
    e.preventDefault();
    playerPlane.toShooting();
    addListener(playPage, "mousemove", playShootingmove);
    addListener(playPage, "mouseup", playShootingup);
}
function playShootingmove(e) {
    if (bulletbu == true) {
        e.preventDefault();
        playerPlane.toShooting();
    }
}
function playShootingup(e) {
    bulletbu = false;
    playPage.removeEventListener("mousemove", playShootingmove);
    playPage.removeEventListener("mouseup", playShootingup);
}

function keyupspace(e) {
    if (e.key == " ") {
        if (keyspace == false) {
            keyspace = true;
            clearInterval(timer);
            playPage.style.cursor = "";
            playPage.removeEventListener("mousedown", playShooting);
            playPage.removeEventListener("mousemove", playMoveFu);
        } else {
            keyspace = false;
            timer = setInterval(setInter, 10);
            playPage.style.cursor = "none";
            addListener(playPage, "mousedown", playShooting);
            addListener(playPage, "mousemove", playMoveFu);
        }
    }
    if (e.key == "Escape") {
        startPage.style.display = "";
        playPage.style.display = "";
        playPage.style.cursor = "";
        playMose.parentNode.style.display = "";
        clearInterval(timer);
        playPage.removeEventListener("mousemove", playShootingmove);
    }
}
function contextmenuFu(e) {
    if (e.target.innerText == "") {
        e.preventDefault();
        e.cancleBubble = true;
        if (contextmenudivt >= 2000) {
            contextmenudiv = true;
            contextmenudivt = 0;
        }
    }
}
addListener(document, "keyup", keyupspace);
addListener(document, "contextmenu", contextmenuFu);
